<?php
if (!defined('APPPATH')) exit('No direct script access allowed');

include_once(APPPATH.'libraries/api/core/MySQL.php');
include_once(APPPATH.'libraries/api/core/Services.php');

class Newsletter extends Services {
    
	public static function newsletterGet($newsletter_id) {
		if(parent::checkArguments($newsletter_id)) {
			$query = "SELECT `newsletter_id`, `name`, `cover`, `content` FROM `newsletter` 
					  WHERE `newsletter_id` = $newsletter_id AND `deleted` != 1; ";
			$result = MySQL::selectGetRow($query);
			if(parent::check($result)) {
				return $result;
			} else {
				return array();
			}
		} else {
			return array();
		}
	}
        
	public static function newsletterSet($language_id, $name, $cover, $content) {
		if(parent::checkArguments(array($language_id, $name, $cover, $content))) {
			$query = "INSERT INTO `newsletter` (`language_id`, `name`, `cover`, `content`, `deleted`) 
					  VALUES ($language_id, '$name', '$cover', '$content', 0); ";
			$result = MySQL::update($query);
			if(parent::check($result)) {
				return true;
			} else {
				return false;
			}
		} else {
			return false;
		}
	}
	
	public static function newsletterUpdate($newsletter_id, $language_id, $name, $cover, $content) {
		if(parent::checkArguments(array($newsletter_id, $language_id, $name, $cover, $content))) {
			$query = "UPDATE `newsletter` SET `language_id` = '$language_id', 
                                                          `name` = '$name', 
                                                          `cover` = '$cover', 
                                                          `content` = '$content', 
                                  WHERE `newsletter_id` = $newsletter_id;";
			$result = MySQL::update($query);
			if(parent::check($result)) {
				return true;
			} else {
				return false;
			}
		} else {
			return false;
		}
	}

	public static function newsletterDelete($newsletter_id) {
		if(parent::checkArguments($newsletter_id)) {
			$query = "UPDATE `newsletter` SET `deleted` = 1 WHERE `newsletter_id` = $newsletter_id;";
			$result = MySQL::update($query);
			if(parent::check($result)) {
				return true;
			} else {
				return false;
			}
		} else {
			return false;
		}
	}
	
	public static function newslettersList($language_id) {
		if(parent::checkArguments($language_id)) {
			$query = "SELECT `newsletter_id`, `name`, `cover`, `content` 
                                  FROM `newsletter` WHERE `language_id` = $language_id AND `deleted` != 1; ";
			$result = MySQL::selectGetArray($query);
			if(parent::check($result)) {
				return $result;
			} else {
				return array();
			}
		} else {
			return array();
		}
	}
        
	public static function newslettersAttributesGet() {
		$query = "SELECT `headline`, `description`, `address`, `contact` FROM `newsletters_attributes`WHERE `deleted` != 1; ";
		$result = MySQL::selectGetRow($query);
		if(parent::check($result)) {
			return $result;
		} else {
			return array();
		}
	}
        
	public static function newsletterSubscribe($email) {
		if(parent::checkArguments($email)) {
			$query = "DELETE FROM `newsletters_subscribe` WHERE `email` = '$email'; INSERT INTO `newsletters_subscribe` (`email`) VALUES ('$email');";
			$result = MySQL::update($query);
			if(parent::check($result)) {
				return true;
			} else {
				return false;
			}
		} else {
			return false;
		}
	}
        
	public static function newsletterUnsubscribe($email) {
		if(parent::checkArguments($email)) {
			$query = "DELETE FROM `newsletters_subscribe` WHERE `email` = '$email');";
			$result = MySQL::update($query);
			if(parent::check($result)) {
				return true;
			} else {
				return false;
			}
		} else {
			return false;
		}
	}
        
	public static function newsletterSubscribersList() {
                    $query = "SELECT `newsletters_subscribe_id`, `email` FROM `newsletters_subscribe` WHERE `deleted` != 1; ";
                    $result = MySQL::selectGetArray($query);
                    if(parent::check($result)) {
                            return $result;
                    } else {
                            return array();
                    }
	}

}

?>